<html>
<head><meta charset="utf-8"><title>inferred types `_` choking on default type params · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html">inferred types `_` choking on default type params</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="235025879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235025879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235025879">(Apr 17 2021 at 23:08)</a>:</h4>
<p>I noticed a curious interaction today that I can't find a documented rationale for. Default type parameters don't work with inferred types/type holes, <code>_</code>. See the following program:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="nc">HashMap</span><span class="o">&lt;</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)].</span><span class="n">into_iter</span><span class="p">().</span><span class="n">collect</span><span class="p">();</span><span class="w"> </span><span class="c1">// ok</span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="nc">HashMap</span><span class="o">&lt;</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)].</span><span class="n">into_iter</span><span class="p">().</span><span class="n">collect</span><span class="p">();</span><span class="w"> </span><span class="c1">// fails</span>
</code></pre></div>
<p>the first line omits the final type parameter, and gets inferred properly. the second line explicitly tells the compiler to infer the parameter, and gives a type inference error. is this intended?</p>



<a name="235025950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235025950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235025950">(Apr 17 2021 at 23:09)</a>:</h4>
<p>reminder that HashMap is defined as <code>pub struct HashMap&lt;K, V, S = RandomState&gt; {</code></p>



<a name="235030019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235030019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235030019">(Apr 18 2021 at 00:26)</a>:</h4>
<p>It's definitely how default type parameters are spec'd to work.</p>



<a name="235030115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235030115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235030115">(Apr 18 2021 at 00:28)</a>:</h4>
<p><code>HashMap&lt;_, _&gt;</code> is explicitly <code>HashMap&lt;_, _, RandomState&gt;</code>, not another <code>_</code>.</p>



<a name="235030206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235030206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235030206">(Apr 18 2021 at 00:30)</a>:</h4>
<p>I think it's sortof stuck that way, because otherwise adding defaults would make everything break with "type argument not specified" in existing code.</p>



<a name="235030533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235030533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235030533">(Apr 18 2021 at 00:35)</a>:</h4>
<p>I think <code>HashMap&lt;_, _, _&gt;</code> should also fall back to the explicit default in the type param, though</p>



<a name="235031288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235031288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235031288">(Apr 18 2021 at 00:49)</a>:</h4>
<p>how would you express "use type inference" then?</p>



<a name="235031298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235031298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235031298">(Apr 18 2021 at 00:50)</a>:</h4>
<p>hm, well, I mean <em>fall back</em></p>



<a name="235031348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235031348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235031348">(Apr 18 2021 at 00:50)</a>:</h4>
<p>just like <code>let i: _ = 3;</code> falls back to i32, but can use some other type too?</p>



<a name="235031362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235031362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235031362">(Apr 18 2021 at 00:50)</a>:</h4>
<p>so not sure I understand the question</p>



<a name="235036814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235036814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235036814">(Apr 18 2021 at 02:34)</a>:</h4>
<p>I definitely think some sort of hinting like that would be great.  We've wanted a way to nudge indexing to <code>usize</code> (so we can also allow more widths) for a while, for example.  Dunno exactly what that should look like, though.</p>



<a name="235054980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235054980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235054980">(Apr 18 2021 at 08:15)</a>:</h4>
<p>Hasn't "fallback to default type param" been discussed for years? Or am I imagining that?</p>



<a name="235055010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235055010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235055010">(Apr 18 2021 at 08:15)</a>:</h4>
<p>e.g. it is mentioned here <a href="https://github.com/rust-lang/rust/issues/27242">https://github.com/rust-lang/rust/issues/27242</a></p>



<a name="235076565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inferred%20types%20%60_%60%20choking%20on%20default%20type%20params/near/235076565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inferred.20types.20.60_.60.20choking.20on.20default.20type.20params.html#235076565">(Apr 18 2021 at 14:41)</a>:</h4>
<p>yeah I think it has been</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>